//=============================================================================
// P_S_Big_Nade_m
//=============================================================================
class P_S_Big_Nade_m extends P_S_Big_Nade;

var () int HealBoostAmount;
var () int ShieldBoostAmount;


var float AverageBlowTime;
var float CurrentFlewTime;
//var float ExplodeTimer;


function PostNetBeginPlay()
{
	SetTimer(AverageBlowTime, true);
}



simulated function Explode(vector HitLocation, vector HitNormal)
{
	/*local Controller C;
    local PlayerController  LocalPlayer;
	local byte i;*/
	
	
	CurrentFlewTime+=AverageBlowTime;
	//log(CurrentFlewTime);

	Spawn(Class'UnitedMut_v54.medicExplosion',,, HitLocation, rotator(HitNormal));
	//PlaySound(ExplodeSounds[rand(1)],,2.0);
	/*
	if ( EffectIsRelevant(Location,false) )
	{
		Spawn(Class'UnitedMut_v54.medicExplosion',,, HitLocation, rotator(HitNormal));
		//Spawn(ExplosionDecal,self,,HitLocation, rotator(-HitNormal));
		
       // Spawn(Class'UnitedMut_v54.EXHealingFX',,, HitLocation + HitNormal*20, rotator(HitNormal));
	}
*/
	
	BlowUp(HitLocation);

}

simulated function BlowUp(vector HitLocation)
{
	HurtRadius(Damage,DamageRadius, MyDamageType, MomentumTransfer, HitLocation );
	if ( Role == ROLE_Authority )
		MakeNoise(1.0);
}

function Timer()
{
	if(CurrentFlewTime>ExplodeTimer)
	{
		bHasExploded=true;
		Destroy();
	}
	else
	{
		Explode(Location, vect(0,0,1));
	}
}

simulated singular function Touch(Actor Other)
{
	//log("touch");
	SetTimer(AverageBlowTime, true);
}

simulated function HurtRadius( float DamageAmount, float DamageRadius, class<DamageType> DamageType, float Momentum, vector HitLocation )
{
    local actor Victims;
    local float damageScale, dist;
    local vector dirs;
	local int NumKilled;
	local KFMonster KFMonsterVictim;
	local Pawn P;
	local KFPawn KFP;
	local array<Pawn> CheckedPawns;
	local int i;
	local bool bAlreadyChecked;
    local KFPlayerReplicationInfo PRI;
    local int MedicReward;
	local KFHumanPawn Healed;
        local float HealSum; // for modifying based on perks

//log("medic: hurt radius");
    if ( bHurtEntry )
        return;

    bHurtEntry = true;

    foreach CollidingActors (class 'Actor', Victims, DamageRadius, HitLocation)
    {
        // don't let blast damage affect fluid - VisibleCollisingActors doesn't really work for them - jag
        if( (Victims != self) && (Hurtwall != Victims) && (Victims.Role == ROLE_Authority) && !Victims.IsA('FluidSurfaceInfo')
		 && ExtendedZCollision(Victims)==None )
        {
            dirs = Victims.Location - HitLocation;
            dist = FMax(1,VSize(dirs));
            dirs = dirs/dist;
            damageScale = 1 - FMax(0,(dist - Victims.CollisionRadius)/DamageRadius);
            if ( Instigator == None || Instigator.Controller == None )
                Victims.SetDelayedDamageInstigatorController( InstigatorController );
            if ( Victims == LastTouched )
                LastTouched = None;

			P = Pawn(Victims);

			if( P != none )
			{
		        for (i = 0; i < CheckedPawns.Length; i++)
				{
		        	if (CheckedPawns[i] == P)
					{
						bAlreadyChecked = true;
						break;
					}
				}

				if( bAlreadyChecked )
				{
					bAlreadyChecked = false;
					P = none;
					continue;
				}

                KFMonsterVictim = KFMonster(Victims);

    			if( KFMonsterVictim != none && KFMonsterVictim.Health <= 0 )
    			{
                    KFMonsterVictim = none;
    			}

                KFP = KFPawn(Victims);

                if( KFMonsterVictim != none )
                {
                    damageScale *= KFMonsterVictim.GetExposureTo(HitLocation/*Location + 15 * -Normal(PhysicsVolume.Gravity)*/);
                }
                else if( KFP != none )
                {
				    damageScale *= KFP.GetExposureTo(HitLocation/*Location + 15 * -Normal(PhysicsVolume.Gravity)*/);
                }

				CheckedPawns[CheckedPawns.Length] = P;

				if ( damageScale <= 0)
				{
					P = none;
					continue;
				}
				else
				{
					//Victims = P;
					P = none;
				}
			}

            if (KFHumanPawn(Victims) != none)
             {
               Healed = KFHumanPawn(Victims);
               if (Instigator != none && Healed != none && Healed.Health > 0 &&
                   Healed.Health <  Healed.HealthMax)
                {
                   MedicReward = HealBoostAmount;

    	    	   PRI = KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo);

    		   if ( PRI != none && PRI.ClientVeteranSkill != none )
    		    {
    			MedicReward *= PRI.ClientVeteranSkill.Static.GetHealPotency(PRI);
    		    }

                   MedicReward *= damageScale; /* Reduce healing strength for then distance from explotion center increases */

                   HealSum = MedicReward;

    		   if ( (Healed.Health + Healed.healthToGive + MedicReward) > Healed.HealthMax )
    		    {
                      MedicReward = Healed.HealthMax - (Healed.Health + Healed.healthToGive);
    		 	 if ( MedicReward < 0 )
    			  {
    				MedicReward = 0;
    			  }
    		    }

                   Healed.GiveHealth(HealSum, Healed.HealthMax);
                   if (Healed.ShieldStrength >= 0)
                    {
                      if (Healed.ShieldStrength < 75)
                         Healed.AddShieldStrength(ShieldBoostAmount * damageScale * PRI.ClientVeteranSkill.Static.GetHealPotency(PRI) );
                    }

     		   if ( PRI != None )
    		    {
    			if ( MedicReward > 0 && KFSteamStatsAndAchievements(PRI.SteamStatsAndAchievements) != none )
    			{
    				KFSteamStatsAndAchievements(PRI.SteamStatsAndAchievements).AddDamageHealed(MedicReward, true);
					SRPlayerReplicationInfo(Instigator.PlayerReplicationInfo).outShld += ShieldBoostAmount * damageScale * PRI.ClientVeteranSkill.Static.GetHealPotency(PRI);
					SRPlayerReplicationInfo(Healed.PlayerReplicationInfo).inShld += ShieldBoostAmount * damageScale * PRI.ClientVeteranSkill.Static.GetHealPotency(PRI);
					SRPlayerReplicationInfo(Instigator.PlayerReplicationInfo).outHP += MedicReward;
					SRPlayerReplicationInfo(Healed.PlayerReplicationInfo).inHP += MedicReward;
    			}

                        // Give the medic reward money as a percentage of how much of the person's health they healed
    			  MedicReward = int((FMin(float(MedicReward),Healed.HealthMax)/Healed.HealthMax) * 60); // Increased to 80 in Balance Round 6, reduced to 60 in Round 7

    			PRI.Score += MedicReward;
    			PRI.ThreeSecondScore += MedicReward;

    			PRI.Team.Score += MedicReward;

    			if ( KFHumanPawn(Instigator) != none )
    			{
    				KFHumanPawn(Instigator).AlphaAmount = 255;
    			}

                        if( MP7MMedicGun(Instigator.Weapon) != none )
                        {
                           MP7MMedicGun(Instigator.Weapon).ClientSuccessfulHeal(Healed.PlayerReplicationInfo.PlayerName);
                        }
    		    }
                  //Spawn(Class'UnitedMut_v54.EXHealingFX',,, Victims.Location + dirs*20, rotator(dirs));
                }
             }
            else
            {
              Victims.TakeDamage
              (
                 damageScale * DamageAmount,
                 Instigator,
                 Victims.Location - 0.5 * (Victims.CollisionHeight + Victims.CollisionRadius) * dirs,
                 (damageScale * Momentum * dirs),
                 DamageType
              );
              if (Vehicle(Victims) != None && Vehicle(Victims).Health > 0)
                 Vehicle(Victims).DriverRadiusDamage(DamageAmount, DamageRadius, InstigatorController, DamageType, Momentum, HitLocation);

			if( Role == ROLE_Authority && KFMonsterVictim != none && KFMonsterVictim.Health <= 0 )
              {
                 NumKilled++;
              }
            }
        }
    }
    if ( (LastTouched != None) && (LastTouched != self) && (LastTouched.Role == ROLE_Authority) && !LastTouched.IsA('FluidSurfaceInfo') )
    {
        Victims = LastTouched;
        LastTouched = None;
        dirs = Victims.Location - HitLocation;
        dist = FMax(1,VSize(dirs));
        dirs = dirs/dist;
        damageScale = FMax(Victims.CollisionRadius/(Victims.CollisionRadius + Victims.CollisionHeight),1 - FMax(0,(dist - Victims.CollisionRadius)/DamageRadius));
        if (KFHumanPawn(Victims) != none)
         {
            Healed = KFHumanPawn(Victims);
            if (Instigator != none && Healed != none && Healed.Health > 0 &&
                Healed.Health <  Healed.HealthMax)
             {
                MedicReward = HealBoostAmount;

    		PRI = KFPlayerReplicationInfo(Instigator.PlayerReplicationInfo);

    		if ( PRI != none && PRI.ClientVeteranSkill != none )
    		{
    			MedicReward *= PRI.ClientVeteranSkill.Static.GetHealPotency(PRI);
    		}

                MedicReward *= damageScale; /* Reduce healing strength for then distance from explotion center increases */

            HealSum = MedicReward;

    		if ( (Healed.Health + Healed.healthToGive + MedicReward) > Healed.HealthMax )
    		{
                MedicReward = Healed.HealthMax - (Healed.Health + Healed.healthToGive);
    			if ( MedicReward < 0 )
    			{
    				MedicReward = 0;
    			}
    		}

            Healed.GiveHealth(HealSum, Healed.HealthMax);
            if (Healed.ShieldStrength >= 0)
             {
               if (Healed.ShieldStrength < 75)
                 Healed.AddShieldStrength(ShieldBoostAmount * damageScale * PRI.ClientVeteranSkill.Static.GetHealPotency(PRI) );
             }

     		if ( PRI != None )
    		{
    			if ( MedicReward > 0 && KFSteamStatsAndAchievements(PRI.SteamStatsAndAchievements) != none )
    			{
    				KFSteamStatsAndAchievements(PRI.SteamStatsAndAchievements).AddDamageHealed(MedicReward, true);
					SRPlayerReplicationInfo(Instigator.PlayerReplicationInfo).outShld += ShieldBoostAmount * damageScale * PRI.ClientVeteranSkill.Static.GetHealPotency(PRI);
					SRPlayerReplicationInfo(Healed.PlayerReplicationInfo).inShld += ShieldBoostAmount * damageScale * PRI.ClientVeteranSkill.Static.GetHealPotency(PRI);
					SRPlayerReplicationInfo(Instigator.PlayerReplicationInfo).outHP += MedicReward;
					SRPlayerReplicationInfo(Healed.PlayerReplicationInfo).inHP += MedicReward;
    			}

                // Give the medic reward money as a percentage of how much of the person's health they healed
    			MedicReward = int((FMin(float(MedicReward),Healed.HealthMax)/Healed.HealthMax) * 60); // Increased to 80 in Balance Round 6, reduced to 60 in Round 7

    			PRI.Score += MedicReward;
    			PRI.ThreeSecondScore += MedicReward;

    			PRI.Team.Score += MedicReward;

    			if ( KFHumanPawn(Instigator) != none )
    			{
    				KFHumanPawn(Instigator).AlphaAmount = 255;
    			}

                if( MP7MMedicGun(Instigator.Weapon) != none )
                {
                    MP7MMedicGun(Instigator.Weapon).ClientSuccessfulHeal(Healed.PlayerReplicationInfo.PlayerName);
                }
    		}
               //Spawn(Class'UnitedMut_v54.EXHealingFX',,, Victims.Location + dirs*20, rotator(dirs));
             }
         }
        else
         {
           if ( Instigator == None || Instigator.Controller == None )
              Victims.SetDelayedDamageInstigatorController(InstigatorController);
           Victims.TakeDamage
           (
             damageScale * DamageAmount,
             Instigator,
             Victims.Location - 0.5 * (Victims.CollisionHeight + Victims.CollisionRadius) * dirs,
             (damageScale * Momentum * dirs),
             DamageType
           );
           if (Vehicle(Victims) != None && Vehicle(Victims).Health > 0)
             Vehicle(Victims).DriverRadiusDamage(DamageAmount, DamageRadius, InstigatorController, DamageType, Momentum, HitLocation);
         }
    }

	if( Role == ROLE_Authority )
    {
        if( NumKilled >= 4 )
        {
            KFGameType(Level.Game).DramaticEvent(0.05);
        }
        else if( NumKilled >= 2 )
        {
            KFGameType(Level.Game).DramaticEvent(0.03);
        }
    }

    bHurtEntry = false;
}

defaultproperties
{
     HealBoostAmount=70
     AverageBlowTime=0.200000
     ExplodeTimer=3.000000
     ExplodeSounds(0)=SoundGroup'KF_InventorySnd.Injector_Fire'
     ExplodeSounds(1)=Sound'KF_InventorySnd.Medkit_Pickup'
     Damage=50.000000
     MyDamageType=Class'UnitedMut_v54.WTFEquipDamTypeLethalInjection'
     StaticMesh=StaticMesh'KillingFloorLabStatics.Lab_ChemContainer'
     DrawScale=0.350000
}
